package com.lagou.orderservicerocket.service;

import com.lagou.orderservicerocket.pojo.Order;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @Authther: mrwu
 * @date:2021/5/16-05
 * @Description:com.lagou.orderservicerocket.service
 */
@Service
@Slf4j
@RocketMQMessageListener(topic = "goods-skill",consumerGroup = "goods-skill-consumer")
public class OrderRocketListenner implements RocketMQListener<String> {
    @Autowired
    RocketMQTemplate rocketMQTemplate;

    @Autowired
    OrderService orderService;

    @Override
    public void onMessage(String s) {
        log.info("接受到消息"+s);
        //todo 写入订单 保存订单
        Order order = new Order();
//        order.setProductId();
        orderService.save(order);
        //mq发送延时队列，检查订单是否支付 设置30s后检查订单是否支付
        Message message = new GenericMessage(order.getId());
        rocketMQTemplate.asyncSend("check-skill-order", message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {

            }

            @Override
            public void onException(Throwable e) {

            }
        },1000,3);

    }
}
